﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AcsCommon;

namespace YiShi.Class
{
    public class GuideLink : DbObject
    {
        private IGuide nextGuide;
        public string QuestionId
        {
            get
            {
                return dr["QuestionId"];
            }
            set
            {
                dr["QuestionId"] = value;
            }
        }
        public string PreGuideId
        {
            get
            {
                return dr["PreGuideId"];
            }
            set
            {
                dr["PreGuideId"] = value;
            }
        }
        public string NextGuideId
        {
            get
            {
                return dr["NextGuideId"];
            }
            set
            {
                dr["NextGuideId"] = value;
            }
        }
        public string Answer
        {
            get
            {
                return dr["Answer"];
            }
            set
            {
                dr["Answer"] = value;
            }
        }
        public string Tip
        {
            get
            {
                return dr["Tip"];
            }
            set
            {
                dr["Tip"] = value;
            }
        }
        public string KnowledgePointIds
        {
            get
            {
                return dr["KnowledgePointIds"];
            }
            set
            {
                dr["KnowledgePointIds"] = value;
            }
        }
        public string KnowledgePointNames
        {
            get
            {
                return dr["KnowledgePointNames"];
            }
            set
            {
                dr["KnowledgePointNames"] = value;
            }
        }
        public GuideLink(DbAccess db)
            : base(db, "Admin.GuideLink")
        {
        }
        public override void Update()
        {
            base.Update("Admin.GuideLink");
            if (!String.IsNullOrEmpty(this.KnowledgePointIds))
            {
                List<string> sqls = new List<string>();
                List<int> effectchecks = new List<int>();
                sqls.Add(Sql.Format("delete from QuestionKnowledgePoint where (QuestionId = '{0}') and (GuideLinkId = '{1}')", this.QuestionId, this.Id));
                effectchecks.Add(-1);
                string[] ids = this.KnowledgePointIds.Split(',');
                for (int i = 0; i < ids.Length; i++)
                {
                    sqls.Add(Sql.Format("insert into QuestionKnowledgePoint(QuestionId, GuideLinkId, KnowledgePointId) values ('{0}', '{1}', '{2}')", this.QuestionId, this.Id, ids[i]));
                    effectchecks.Add(1);
                }
                db.ExecuteSqlTrans(sqls, effectchecks);
            }

        }
        public IGuide NextGuide()
        {
            if (String.IsNullOrEmpty(this.Id))
                throw new Exception("The id of the question is null!");
            if (this.nextGuide == null)
            {
                if (dr["QuestionId"] == dr["NextGuideId"])
                {
                    Question q = new Question(this.db);
                    q.FillFromDb(dr["NextGuideId"]);
                    this.nextGuide = q;
                }
                else
                {
                    Guide g = new Guide(this.db);
                    g.FillFromDb(dr["NextGuideId"]);
                    this.nextGuide = g;
                }
            }
            return this.nextGuide;
        }
        public void FillFromDb(string guideId, string answer)
        {
            DataRecord dr = this.db.SelectRecord(this.fullTableName, "*", "PreGuideId", guideId, "Answer", answer);
            dr.FillObject(this);
        }
    }
}
